package com.fqgj.sentry.policy.server.policy;

import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.policy.dao.PcColumnDao;
import com.fqgj.sentry.policy.dao.PcPolicyDao;
import com.fqgj.sentry.policy.domain.policy.Policy;
import com.fqgj.sentry.policy.entity.PcPolicyEntity;
import com.fqgj.sentry.policy.dao.PcPolicySetsDao;
import com.fqgj.sentry.policy.entity.PcColumnEntity;
import com.fqgj.sentry.policy.entity.PcPolicySetsEntity;
import com.fqgj.sentry.policy.enums.*;
import com.fqgj.sentry.policy.exception.PolicyErrorCodeEnums;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2017/7/19
 * Time: 下午5:43
 */
@Service
@Transactional
public class AddPolicyService {

    @Autowired
    private PcPolicyDao pcPolicyDao;

    @Autowired
    private PcColumnDao pcColumnDao;

    @Autowired
    private PcPolicySetsDao policySetsDao;

    public Boolean execute(Long executorId, Long policySetsId, Long merchantId, Policy policy) {
        if (executorId == null
                || policySetsId == null
                || policy.getMatchingTypeEnum() == null
                || policy.getTypeEnum() == null) {
            throw new BizException(PolicyErrorCodeEnums.add_policy_error, "请完善信息");
        }
        Integer maxCode = pcPolicyDao.selectMaxCode(policySetsId);
        Integer maxSort = pcPolicyDao.selectMaxSort(policySetsId);
        String nowCode = "1";
        if (maxCode != null) {
            nowCode = (maxCode + 1) + "";
        }
        if (policy.getTypeEnum() == PolicyTypeEnum.rules) {
            if (policy.getMatchingTypeEnum() == PolicyMatchingTypeEnum.score) {
                throw new BizException(PolicyErrorCodeEnums.add_policy_error, "匹配类型选择异常，请重新选择");
            }
        }
        //判读 是否为 评分卡     ------字段维护
        if (policy.getTypeEnum() == PolicyTypeEnum.grade) {
            if (StringUtils.isBlank(policy.getColumn()) || StringUtils.isBlank(policy.getColumnName())) {
                throw new BizException(PolicyErrorCodeEnums.policy_grade_not_exist_column_error, "评分卡时，字段和字段名不能为空~");
            }
            if (StringUtils.isBlank(policy.getBaseScore())) {
                throw new BizException(PolicyErrorCodeEnums.policy_grade_not_exist_column_error);
            }
            //新增到字段表
            PcColumnEntity columnEntity = new PcColumnEntity();
            PcPolicySetsEntity policySetsEntity = policySetsDao.selectByPrimaryKey(policySetsId);
            columnEntity.setEventType(ColumnEventTypeEnum.not_all_event.getType())
                    .setMerchantId(merchantId)
                    .setEventIds(policySetsEntity.getEventId().toString())
                    .setColumnCode(policy.getColumn())
                    .setDisplayName(policy.getColumnName())
                    .setDataType(ColumnDateTypeEnum.decimals_data.getType())
                    .setStatus(ColumnStatusEnum.field_grade.getType());
            pcColumnDao.insert(columnEntity);
        }
        //权重匹配  权重必须配置
        if (policy.getMatchingTypeEnum() == PolicyMatchingTypeEnum.weight_match) {
            if (StringUtils.isBlank(policy.getWeightRange())) {
                throw new BizException(PolicyErrorCodeEnums.policy_weight_not_exist_error);
            }
        }
        PcPolicyEntity pcPolicyEntity = new PcPolicyEntity();
        BeanUtils.copyProperties(policy, pcPolicyEntity);
        if (policy.getTypeEnum() == PolicyTypeEnum.grade) {
            if (StringUtils.isNotEmpty(policy.getColumn())) {
                pcPolicyEntity.setColumnCode(policy.getColumn());
            }
            if (StringUtils.isNotEmpty(policy.getBaseScore())) {
                pcPolicyEntity.setBaseScore(BigDecimal.valueOf(Double.valueOf(policy.getBaseScore())));
            }
        }

        pcPolicyEntity.setCode(nowCode).setSort(maxSort == null ? 1 : maxSort + 1)
                .setMatchingType(policy.getMatchingTypeEnum().getType())
                .setType(policy.getTypeEnum().getType())
                .setPolicySetsId(policySetsId)
                .setExecutorId(executorId);

        return pcPolicyDao.insert(pcPolicyEntity) != null;
    }
}
